package com.cgc.venus.server.ci.controller;

import com.cgc.venus.server.ci.inter.CIService;
import com.cgc.venus.server.ci.model.dao.PipelineDao;
import com.cgc.venus.server.ci.model.vo.JdkVO;
import com.cgc.venus.server.ci.model.vo.PipelineBuildLogVO;
import com.cgc.venus.server.ci.model.vo.PipelineParaVO;
import com.cgc.venus.server.ci.model.vo.PipelineVO;
import com.cgc.venus.server.core.bean.response.VenusResponse;
import com.cgc.venus.server.core.bean.response.VenusResponseStatus;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 业哥 on 2018/11/13
 */
@Api(value = "持续集成接口",description="持续集成接口")
@RequestMapping("/ci-admin")
@RestController
public class CIController {
    @Autowired
    private CIService ciService;

    @ApiOperation("查询集成流水线(单行记录)")
    @RequestMapping(value = "/pipeline/row/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object m0(@PathVariable("id") String id) {
        try {
            PipelineVO pv=ciService.findPipelineByIdWithNoDetail(id);
            VenusResponse<PipelineVO> response=new VenusResponse<PipelineVO>(VenusResponseStatus.OK,"",pv);
            return  response;
        }catch (Exception e){
            return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"查询失败:"+e.getMessage(),"");
        }
    }

    @ApiOperation("查询集成流水线")
    @RequestMapping(value = "/pipeline", method = RequestMethod.GET)
    @ResponseBody
    public Object m1(@RequestParam("key") String key,@RequestParam("namespace") String namespace,@RequestParam("pageNum") Integer pageNum,@RequestParam("rowNum") Integer rowNum) {
        try {
            PageInfo<PipelineVO> pageInfo=ciService.findPipelinesByKey(key,namespace,pageNum,rowNum);
            VenusResponse<PageInfo<PipelineVO>> response=new VenusResponse<PageInfo<PipelineVO>>(VenusResponseStatus.OK,"",pageInfo);
            return  response;
        }catch (Exception e){
            return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"查询失败:"+e.getMessage(),"");
        }
    }

    @ApiOperation("创建集成流水线")
    @RequestMapping(value = "/pipeline", method = RequestMethod.POST)
    public Object m2(@RequestBody PipelineVO pipeline) {
        try {
            if(pipeline.getPipelineName()==null||pipeline.getPipelineName().equals("")){
                return new VenusResponse<Object>(VenusResponseStatus.VENUS_EXCEPTION,"流水线名称不能为空!");
            }
            if(pipeline.getKeepBuildDay()<=0){
                return new VenusResponse<Object>(VenusResponseStatus.VENUS_EXCEPTION,"流水线 build保持天数至少为1!");
            }
            if(pipeline.getKeepBuildNumber()<=0){
                return new VenusResponse<Object>(VenusResponseStatus.VENUS_EXCEPTION,"流水线 build记录数数至少为1!");
            }
            int i=ciService.addPipeline(pipeline);
            if(i==1){
                return new VenusResponse<String>(VenusResponseStatus.OK,"创建流水线成功!","1");
            }else{
                return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"创建流水线失败:","");
            }
        } catch (Exception e) {
            return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,e.getMessage(),"");
        }
    }

    @ApiOperation("修改集成流水线")
    @RequestMapping(value = "/pipeline", method = RequestMethod.PATCH)
    @ResponseBody
    public Object m3(@RequestBody PipelineVO pipeline) {
        try {
            if(pipeline.getPipelineName()==null||pipeline.getPipelineName().equals("")){
                return new VenusResponse<Object>(VenusResponseStatus.VENUS_EXCEPTION,"流水线名称不能为空!");
            }
            if(pipeline.getKeepBuildDay()<=0){
                return new VenusResponse<Object>(VenusResponseStatus.VENUS_EXCEPTION,"流水线 build保持天数至少为1!");
            }
            if(pipeline.getKeepBuildNumber()<=0){
                return new VenusResponse<Object>(VenusResponseStatus.VENUS_EXCEPTION,"流水线 build记录数数至少为1!");
            }
            int i=ciService.updatePipeline(pipeline);
            if(i==1){
                return new VenusResponse<String>(VenusResponseStatus.OK,"修改流水线成功!","1");
            }else{
                return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"修改流水线失败:","");
            }
        } catch (Exception e) {
            return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,e.getMessage(),"");
        }
    }

    @ApiOperation("删除集成流水线")
    @RequestMapping(value = "/pipeline", method = RequestMethod.DELETE)
    @ResponseBody
    public Object m4(@RequestParam("id") String id,@RequestParam("pipelineNameEn") String pipelineNameEn) {
        try {
            int row=ciService.deletePipeline(id,pipelineNameEn);
            if(row==1){
                return new VenusResponse<String>(VenusResponseStatus.OK,"删除成功!","");
            }else{
                return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"删除失败!","");
            }
        }catch (Exception e){
            return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"删除流水线失败:"+e.getMessage(),"");
        }
    }

    @ApiOperation("查询集成流水线单条明细")
    @RequestMapping(value = "/pipeline/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object m5(@PathVariable("id") String id) {
        try{
            PipelineVO pipeline=ciService.findPipelineById(id);
            VenusResponse<PipelineVO> response=new VenusResponse<PipelineVO>(VenusResponseStatus.OK,"",pipeline);
            return  response;
        }catch (Exception e){
            return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"查询失败:"+e.getMessage(),"");
        }
    }

    @ApiOperation("集成流水线构建")
    @RequestMapping(value = "/pipeline/build/{id}/{pipelineNameEn}", method = RequestMethod.POST)
    public Object m6(@PathVariable("id") String id,
                      @PathVariable("pipelineNameEn") String pipelineNameEn,
                      @RequestBody List<PipelineParaVO> pipelineParaVOs) {
        try {
            if(id==null||id.equals("")){
                return new VenusResponse<Object>(VenusResponseStatus.VENUS_EXCEPTION,"流水线名称不能为空!");
            }
            int i=ciService.buildPipeline(id,pipelineNameEn,pipelineParaVOs);
            if(i==0){
                return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"流水线构建失败","");
            }else{
                return new VenusResponse<Integer>(VenusResponseStatus.OK,"",i);
            }
        } catch (Exception e) {
            return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"流水线构建失败:"+e.getMessage(),"");
        }
    }

    @ApiOperation("集成流水线构建日志(当前执行)")
    @RequestMapping(value = "/pipeline/build/log/{id}/{pipelineNameEn}/{buildNum}", method = RequestMethod.GET)
    public Object m7(@PathVariable("id") String id,
                      @PathVariable("pipelineNameEn") String pipelineNameEn,
                      @PathVariable("buildNum") int buildNum) {
        try {

            if(id==null||id.equals("")){
                return new VenusResponse<Object>(VenusResponseStatus.VENUS_EXCEPTION,"流水线名称不能为空!");
            }
            PipelineBuildLogVO pipelineBuildLog=ciService.findPipelineBuildLog(id,pipelineNameEn,buildNum);
            if(pipelineBuildLog==null){
                return new VenusResponse<String>(VenusResponseStatus.OK,"","no log record");
            }
            VenusResponse<PipelineBuildLogVO> response=new VenusResponse<PipelineBuildLogVO>(VenusResponseStatus.OK,"",pipelineBuildLog);
            return  response;
        } catch (Exception e) {
            return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"流水线日志获取失败:"+e.getMessage(),"");
        }
    }

    @ApiOperation("集成流水线构建历史日志")
    @RequestMapping(value = "/pipeline/build/log/history/{id}/{pipelineNameEn}/{buildNum}", method = RequestMethod.GET)
    public Object m8(@PathVariable("id") String id,
                     @PathVariable("pipelineNameEn") String pipelineNameEn,
                     @PathVariable("buildNum") int buildNum) {
        try {
            if(id==null||id.equals("")){
                return new VenusResponse<Object>(VenusResponseStatus.VENUS_EXCEPTION,"流水线名称不能为空!");
            }
            List<PipelineBuildLogVO> pipelineBuildLog=ciService.findPipelineBuildLogs(id, pipelineNameEn, buildNum);
            return new VenusResponse<List<PipelineBuildLogVO>>(VenusResponseStatus.OK,"",pipelineBuildLog);
        } catch (Exception e) {
            return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"流水线日志获取失败:"+e.getMessage(),"");
        }
    }

    @ApiOperation("获取jdk配置信息")
    @RequestMapping(value = "/pipeline/config/jdk", method = RequestMethod.GET)
    public Object m9() {
        try {

            List<JdkVO> jdks=ciService.getJdkConfig();
            return new VenusResponse<List<JdkVO>>(VenusResponseStatus.OK,"",jdks);
        } catch (Exception e) {
            return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"jdk配置信息获取失败:"+e.getMessage(),"");
        }
    }

    @ApiOperation("获取流水线参数信息")
    @RequestMapping(value = "/pipeline/config/para/{id}", method = RequestMethod.GET)
    public Object m10(@PathVariable("id") String id) {
        try {

            List<PipelineParaVO> ppvs=ciService.findPipelineParaByPipelineId(id);
            return new VenusResponse<List<PipelineParaVO>>(VenusResponseStatus.OK,"",ppvs);
        } catch (Exception e) {
            return new VenusResponse<String>(VenusResponseStatus.VENUS_EXCEPTION,"流水线参数信息获取失败:"+e.getMessage(),"");
        }
    }
}